package com.zhhtech.zhh.ui.fragment

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.View
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.bean.ShareBean
import com.zhhtech.zhh.databinding.FragmentShareBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseFragment
import com.zhhtech.zhh.ui.ext.permissionRequest
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.helper.loadsir.LoadSirHelper
import com.zhhtech.zhh.ui.utils.AlbumUtils
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb

class ShareFragment : BaseFragment<FragmentShareBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private var mShareBean: ShareBean? = null
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mLoadService = LoadSirHelper.register(rootView!!, listener = ::onReload)
        mView = mLoadService.loadLayout
        bindViewClick(vb.buttonSave, vb.tvWx, vb.tvWxFriend)
        onReload()
    }

    private fun onReload() {
        vb.ivCode.post {
            launchUI({
//                val shareBean = api.userShare().toData() ?: return@launchUI
//                mShareBean = shareBean
//                val bitmap = withContext(Dispatchers.IO) {
//                    ScanUtil.buildBitmap(
//                        shareBean.link, HmsScan.QRCODE_SCAN_TYPE,
//                        vb.ivCode.measuredWidth, vb.ivCode.measuredWidth,
//                        HmsBuildBitmapOption.Creator().setBitmapMargin(0).create()
//                    )
//                }
//                vb.ivCode.setImageBitmap(bitmap)
//                vb.tvCode.text = "邀请码：${UserCache.getUserInfoBean().inviteCode}"
            }, ApiObserver(loadService = mLoadService))
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.tvWx -> getShareAction(v.context)
                ?.setPlatform(SHARE_MEDIA.WEIXIN)
                ?.share()
            vb.tvWxFriend -> getShareAction(v.context)
                ?.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                ?.share()
            vb.buttonSave -> {
                vb.clCode.isDrawingCacheEnabled = true
                vb.clCode.buildDrawingCache()
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
                    AlbumUtils.saveBitmap(requireContext(), vb.clCode.drawingCache, "${System.currentTimeMillis()}.png")
                    "保存成功".toastMessageLong()
                } else {
                    permissionRequest {
                        permission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        onGranted {
                            AlbumUtils.saveBitmap(requireContext(), vb.clCode.drawingCache, "${System.currentTimeMillis()}.png")
                            "保存成功".toastMessageLong()
                        }
                    }
                }
            }
        }
    }

    private fun getShareAction(context: Context): ShareAction? {
        return mShareBean?.let { sysconfig ->
            val web = UMWeb(sysconfig.link)
            web.title = sysconfig.title
            web.setThumb(UMImage(context, R.mipmap.logo))
            web.description = sysconfig.subTitle
            return ShareAction(context as Activity)
                .withMedia(web)
        }
    }
}